package org.jeecg.modules.rooms.mapper;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.rooms.Vo.AlreadyUseLayoutNumberVo;
import org.jeecg.modules.rooms.Vo.LivingSourceStatVo;
import org.jeecg.modules.rooms.entity.CesRooms;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author fendo
 * @since 2023-03-07
 */
public interface CesRoomsMapper extends BaseMapper<CesRooms> {

    List<CesRooms> canUseRooms(String hotelId, String startOf, String endOf);
    List<AlreadyUseLayoutNumberVo> alreadyUseLayoutNumber(String hotelId, Date startOf, Date endOf);

    /**
     * 在住来源分组统计
     * @param hotelId
     * @return
     */
    @Select("<script>select max(source.item_text) as name ,count(0) as count\n" +
            "from bus_dict_item_info source\n" +
            "left join\n" +
            "(SELECT * FROM\n" +
            "(\n" +
            "select x.*,@rownum:=@rownum+1,if(@part=x.room_id,@r:=@r+1,@r:=1) as rank,@part:=x.room_id from (\n" +
            "select e.id,e.create_time,e.booking_order_id,broom.room_id,e.customer_source from bus_rooms_living_order e\n" +
            "inner join bus_booking_rooms broom on broom.booking_orders_id=e.booking_order_id\n" +
            "order by broom.room_id desc,e.create_time desc\n" +
            ") x,(select @rownum:=0,@part:=null,@r:=0) rt\n" +
            ") tmp\n" +
            "where tmp.rank=1) rlo on rlo.customer_source=source.id\n" +
            "left join bus_booking_rooms broom on broom.booking_orders_id=rlo.booking_order_id\n" +
            "left join ces_rooms room on room.id=broom.room_id\n" +
            "where source.dict_id='1639538915239743490'\n" +
            "<if test='hotelId != null and hotelId !=\"\"'> and room.hotel_id = #{hotelId} </if>"+
            "and (room.room_status=3 or room.room_status=4)\n" +
            "group by source.id</script>")
    public List<LivingSourceStatVo> livingSourceStat(@Param("hotelId") String hotelId);


    /**
     * 房态统计
     * @param hotelId
     * @return
     */
    @Select("<script>select\n" +
            "(case room_status when 1 then '空净' when 2 then '空脏' when 3 then '住净' when 4 then '住脏' when 5 then '维修' when 6  then '锁房' else '' end) as name,\n" +
            "count(0) as count\n" +
            "from ces_rooms where 1=1\n and invalid = 0" +
            "<if test='hotelId != null and hotelId !=\"\"'> and hotel_id = #{hotelId} </if>"+
            "group by room_status</script>")
    public List<LivingSourceStatVo> roomLiveStat(@Param("hotelId") String hotelId);


    @Select("SELECT cr.* from bus_room_booking_orders brbo\n" +
            "inner join bus_booking_rooms bbr on brbo.id = bbr.booking_orders_id\n" +
            "inner join ces_rooms cr on bbr.room_id = cr.id\n" +
            "where brbo.booking_status = 1 and brbo.arrival_time > #{nowDateStr} and brbo.arrival_time < #{tomorrowStr}")
    List<CesRooms> getReserveRoom(@Param("nowDateStr") String nowDateStr, @Param("tomorrowStr") String tomorrowStr);
}
